/**
 * 
 */
package com.risetek.tft.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.risetek.tft.context.Constants;
import com.risetek.tft.entity.TManager;
import com.risetek.tft.service.ManagerService;



/**
 * @author hetao
 * 
 */
public class BaseAction  {
	
	 
	
	@Autowired
	private ManagerService managerService;
	
	protected static  Map<String ,String> cfg = null;
	
	protected HttpServletRequest request;  
  //  protected HttpServletResponse response;  
   // protected HttpSession session;  
      
    @ModelAttribute  
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
        this.request = request;  
      //  this.response = response;  
      //  this.session = request.getSession();  
        cfg = (Map<String, String>) request.getAttribute(Constants.SYS_CONFIG_KEY);
    } 
	
	@SuppressWarnings("unchecked")
	@PostConstruct
	public void init(){
		//cfg = (Map<String, String>) request.getAttribute(Constants.SYS_CONFIG_KEY);
	}
	
	protected TManager currentUser(HttpServletRequest request){
		return (TManager)request.getAttribute(Constants.USER_IN_REQUEST);
		/*Subject su = SecurityUtils.getSubject();
		Session session = su.getSession();
		Principal pr = (Principal) su.getPrincipal();
		return managerService.findById(pr.getId().intValue());*/
		
	}

	protected String noright(HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		String requestType = request.getHeader("X-Requested-With");
		if ("XMLHttpRequest".equals(requestType)) {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/json");
			response.setDateHeader("Expires", 0);
			try {
				response.setContentType("json");
				response.getWriter().write("{\"noRight\": true}");
			} catch (IOException e) {
				e.printStackTrace();
			}
			request.setAttribute(Constants.MESSAGE, "没有权限,请联想管理员哦");
			return null;
		} else {
			request.setAttribute(Constants.MESSAGE, "没有权限,请联想管理员哦");
			return Constants.ERROR;
		}
	}

	protected String getAllErrors(List<ObjectError> failures) {
		StringBuffer sb = new StringBuffer();
		for (ObjectError failure : failures) {
			sb.append(failure.getDefaultMessage()).append("|");
		}
		return sb.toString();
	}
	
	
	  

}
